﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DAL
{
   public class UserMngDAL
    {
#pragma warning disable CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
        public static string conn = ConfigurationSettings.AppSettings["con"].ToString();
#pragma warning restore CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
        //通过人脸设备号，卡号进行获取可消费机号
        public static string isVerification_face_Machine_Number(string cardNumber)
        {
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@cardNumber",cardNumber)
            };
            try
            {

                string sql = "SELECT KXFJH FROM RYXX WHERE  RFKH =@cardNumber";
                return SqlHelper.ExecuteScalar(conn, CommandType.Text, sql, sp).ToString(); ;
            }
            catch (Exception ex)
            {
                ApplicationLog.Write("报错isVerification_Machine_Number：" + ex.Message);
                return null;
            }
        }
        //修改心跳
        public static int Update_updateUserInfo(string Machine_Number, string updateUserInfo)
        {
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@Machine_Number",Machine_Number),
                new SqlParameter("@updateUserInfo",updateUserInfo)
            };
            try
            {
                string sql = "UPDATE JHSZ SET updateUserInfo=isnull(updateUserInfo,'')+@updateUserInfo WHERE jh=@Machine_Number";
                return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql, sp);
            }
            catch (Exception ex)
            {
                ApplicationLog.Write("报错Update_equipment：" + ex.Message);
                return 0;
            }
        }
        //修改心跳机号
        public static int Update_equipment(string cardNumber, string xfjh)
        {
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@cardNumber",cardNumber),
                new SqlParameter("@xfjh",xfjh)
            };
            try
            {
                string sql = "UPDATE RYXX SET Inferior_type=1,equipment=@xfjh WHERE RFKH=@cardNumber";
                return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql, sp);
            }
            catch (Exception ex)
            {
                ApplicationLog.Write("报错Update_equipment：" + ex.Message);
                return 0;
            }
        }
        //批量修改心跳协议
        public static int JudgeMachine(string kxfjh, string Agreement1, string Agreement2)
        {
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            string xfjh = kxfjh;
            int sumNum = 0;
            try
            {
                string kxfjh1 = kxfjh.Replace(",", "");
                kxfjh1 = kxfjh1.Replace("，", "");
                int kyxfjh = 0;
                int num = 0;
                int ks = 0;
                if (kxfjh.Length > 0)
                {
                    for (int i = 0; i < (kxfjh1.Length / 3); i++)
                    {

                        if (1 > num)
                        {
                            num++;
                            kyxfjh = Convert.ToInt32(kxfjh.Substring(0, 3));
                            DataTable dataTable = Consumption_restriction(kyxfjh.ToString().PadLeft(3, '0'), "jhsz");
                            if (dataTable.Rows.Count > 0)
                            {
                                string updateUserInfo = dataTable.Rows[0]["updateUserInfo"].ToString();
                                if (updateUserInfo.Length >= 1)
                                {
                                    Update_updateUserInfo(kyxfjh.ToString().PadLeft(3, '0'), Agreement2);
                                }
                                else
                                {
                                    Update_updateUserInfo(kyxfjh.ToString().PadLeft(3, '0'), Agreement1);
                                }
                            }
                            sumNum = 1;
                        }
                        else
                        {
                            ks = ks + 4;
                            //string ss = kxfjh.Substring((0 + ks), 3);
                            kyxfjh = Convert.ToInt32(kxfjh.Substring((0 + ks), 3));
                            DataTable dataTable = Consumption_restriction(kyxfjh.ToString().PadLeft(3, '0'), "jhsz");
                            if (dataTable.Rows.Count > 0)
                            {
                                string updateUserInfo = dataTable.Rows[0]["updateUserInfo"].ToString();
                                if (updateUserInfo.Length >= 1)
                                {
                                    Update_updateUserInfo(kyxfjh.ToString().PadLeft(3, '0'), Agreement2);
                                }
                                else
                                {
                                    Update_updateUserInfo(kyxfjh.ToString().PadLeft(3, '0'), Agreement1);
                                }
                            }
                            sumNum = 1;

                        }

                    }
                }
                return sumNum;
            }
            catch (Exception ex)
            {
                ApplicationLog.Write("报错JudgeMachine：" + ex.Message);
                return sumNum;
            }
        }
        /// <summary>
        /// 获取消费限次，消费天限额,当天流水金额
        /// </summary>
        /// <param name="rfkh"></param>
        /// <returns></returns>
        public static DataTable Consumption_restriction(string rfkh, string type)
        {
            DataTable dataTable = new DataTable();
            try
            {
                SqlParameter[] sp = new SqlParameter[]
                {
                    new SqlParameter("@rfkh",rfkh)
                };
                string sql = string.Empty;
                if (type == "ryxx")
                {
                    sql = "SELECT RFKH,RFYE,RYXM,RFZT,KLB,XC1,XC2,XC3,XC4,XFXE,KXFJH,ZWBH FROM RYXX WHERE RFKH = @rfkh";
                }
                else if (type == "lssj_XFJE")
                {
                    sql = "SELECT ISNULL(SUM(ABS(XFJE)),0) AS XFJE FROM LSSJ WHERE RFKH =@rfkh AND XFSJ BETWEEN Convert(DateTime,Convert(Varchar(10),GetDate(),120)) AND GETDATE()";
                }
                else if (type == "systemset_kxfjh1")
                {
                    sql = "SELECT KXFJH1 FROM systemset";
                }
                else if (type == "OrderNumber_order")
                {
                    sql = "SELECT [Order] FROM OrderNumber where [Order] = @rfkh";
                }
                else if (type == "jhsz")
                {
                    sql = "SELECT updateUserInfo FROM JHSZ where jh = @rfkh";
                }
                else if (type == "ryxx_ryid")
                {
                    sql = "SELECT RFKH,RFYE,RYXM,RFZT,KLB,XC1,XC2,XC3,XC4,XFXE,KXFJH,ZWBH FROM RYXX WHERE ryid = @rfkh";
                }
                return SqlHelper.ExecuteDataSet(conn, CommandType.Text, sql, sp).Tables[0];
            }
            catch (Exception ex)
            {
                ApplicationLog.Write("报错方法Consumption_restriction：" + ex.Message);
                return dataTable;
            }
        }
    }
}
